package com.shenmazong.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shenmazong.mapper.TbStoryMapper;
import com.shenmazong.pojo.TbStory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2022/2/11 15:30
 */

@EnableScheduling
@Component
@Slf4j
public class IndexTask {

    private Boolean isFirst = true;

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    TbStoryMapper tbStoryMapper;

    Date updateTime = null;

    // 10s检查一次
    @Scheduled(fixedRate = 1000*10)
    public void createIndex() {
        if(isFirst) {
            if(!elasticsearchRestTemplate.indexOps(TbStory.class).exists()) {
                log.info("开始创建索引。。。");

                // 创建索引
                elasticsearchRestTemplate.indexOps(TbStory.class).create();

                // 手动创建映射
                log.info("开始创建映射。。。");

                Document mapping = elasticsearchRestTemplate.indexOps(TbStory.class).createMapping();
                elasticsearchRestTemplate.indexOps(TbStory.class).putMapping(mapping);

                log.info("创建索引和映射完成。。。");
            }

            //
            isFirst = false;
        }
    }

    // 全量铜鼓
//    @Scheduled(fixedDelay = 1000*60)
//    public void allSaveToEs() {
//        // 查询所有数据
//        List<TbStory> tbStories = tbStoryMapper.selectList(null);
//        if(tbStories == null || tbStories.size() <= 0) {
//            return;
//        }
//
//        // 同步到es中
//        elasticsearchRestTemplate.save(tbStories);
//        log.info("全量同步完成。。。");
//    }

    // 增量同步
    @Scheduled(fixedDelay = 1000*60, initialDelay = 1000*10)
    public void partSaveToEs() {
        // 判断是否已经通不过
        if(updateTime == null) {
            // 查询所有数据
            QueryWrapper<TbStory> wrapper = new QueryWrapper<>();
            wrapper.lambda().orderByDesc(TbStory::getUpdateTime);
            List<TbStory> tbStories = tbStoryMapper.selectList(null);
            if(tbStories == null || tbStories.size() <= 0) {
                return;
            }

            // 同步到es中
            elasticsearchRestTemplate.save(tbStories);
            log.info("全量同步完成。。。");

            // 获取最后更新时间
            updateTime = tbStories.get(0).getUpdateTime();
        }
        else {
            // 查询没有更新的数据
            QueryWrapper<TbStory> wrapper = new QueryWrapper<>();
            wrapper.lambda().gt(TbStory::getUpdateTime, updateTime).orderByDesc(TbStory::getUpdateTime);
            List<TbStory> tbStories = tbStoryMapper.selectList(null);
            if(tbStories == null || tbStories.size() <= 0) {
                return;
            }

            // 同步到es中
            elasticsearchRestTemplate.save(tbStories);
            log.info("增量同步完成。。。");

            // 获取最后更新时间
            updateTime = tbStories.get(0).getUpdateTime();
        }
    }
}
